import { Injectable } from '@angular/core';
import { Jsonp, Response, Headers, RequestOptions, URLSearchParams } from '@angular/http';
@Injectable()
export class JsonpService {
  count = 0;
  constructor(private jsonp: Jsonp) { }
  get(url: string, params: URLSearchParams): Promise<any> {
    const headers = new Headers({
      'Content-Type': 'application/json;charset=UTF-8',
      'Accept': 'application/json;charset=UTF-8'
    });
    const urlParams = new URLSearchParams();
    if (params != null) {
    urlParams.appendAll(params);
    }
    urlParams.set('callback', `__ng_jsonp__.__req${this.count++}.finished`);
    const options = new RequestOptions({ headers: headers, search: urlParams });
    return this.jsonp.get(url, options)
      .map((rsp: Response) => {
        return rsp.json();
      }).toPromise();
  }
}
